package com.xkdx.guangguang.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSON;
import com.xkdx.caipiao.R;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.presistence.quert.IssueQueryInfo;
import com.xkdx.caipiao.presistence.quert.TypeQueryInfo;
import com.xkdx.caipiao.util.Des3;

public class SharePrefenceUtil {
	private static String TAG = "SharePrefenceUtil";
	Context context;
	SharedPreferences sp;
	SharedPreferences.Editor ep;

	public static String USERINFO = "userinfo";
	public static String LOTTERYTYPE = "lotteryType";
	public static String ISSUEQUERYINFO="issueQueryInfo";

	@SuppressWarnings("static-access")
	public SharePrefenceUtil(Context context, String name) {
		// Log.i(TAG, "-->" + name);
		this.context = context;
		if (context != null) {
			sp = context.getSharedPreferences(name, 0);
			ep = sp.edit();
		}
	}

	public void clear() {
		ep.clear();
		ep.commit();
	}
	
	public void saveObject(String key, Object obj) {
		String alibabaJson = JSON.toJSONString(obj);
		try {
			alibabaJson = Des3.encode(alibabaJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		ep.putString(key, alibabaJson);
		ep.commit();
	}
	
	public TypeQueryInfo getLotteryTypeInfo() {
		TypeQueryInfo info = null;
		String alibabaJson = sp.getString(LOTTERYTYPE, "");
		try {
			alibabaJson = Des3.decode(alibabaJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!alibabaJson.equals("")) {
			info = JSON.parseObject(alibabaJson, TypeQueryInfo.class);
		}
		return info;
	}
	
	public IssueQueryInfo getIssueQueryInfo() {
		IssueQueryInfo info = null;
		String alibabaJson = sp.getString(ISSUEQUERYINFO, "");
		if (!alibabaJson.equals("")) {
			info = JSON.parseObject(alibabaJson, IssueQueryInfo.class);
		}
		return info;
	}
	
	public LoginInfo getUserInfo() {
		LoginInfo info = null;
		String alibabaJson = sp.getString(USERINFO, "");
		if(!"".equals(alibabaJson)){
			try {
				alibabaJson = Des3.decode(alibabaJson);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (!alibabaJson.equals("")) {
			info = JSON.parseObject(alibabaJson, LoginInfo.class);
		}
		return info;
	}

	public String getLocationCityName() {
		return sp.getString("CityName", "北京");
	}

	public void setLocationCityName(String cityName) {
		ep.putString("CityName", cityName);
		ep.commit();
	}

	public void setChoiceCityName(String choiceCityName) {
		ep.putString("ChoiceCityName", choiceCityName);
		ep.commit();
	}

	public String getChoiceCityName() {
		return sp.getString("ChoiceCityName", "");
	}

	public String getCityID() {
		return sp.getString("CityID", "D0101");
	}

	public void setCityID(String cityID) {
		ep.putString("CityID", cityID);
		ep.commit();
	}

	public void setGPS(int lat, int lon) {
		ep.putInt("lat", lat);
		ep.putInt("lon", lon);
		ep.commit();
	}

	public int getLat() {
		return sp.getInt("lat", 0);
	}

	public int getLon() {
		return sp.getInt("lon", 0);
	}

	public void setShopSearch(String key, int distance) {
		ep.putString("key", key);
		ep.putInt("distance", distance);
		ep.commit();
	}

	public String getShopSearchKey() {
		return sp.getString("key", "");
	}

	public int getShopSearchDistance() {
		return sp.getInt("distance", -1);
	}

	public void setUpdateTime(String updateTime) {
		ep.putString("brandUpdateTime", updateTime);
		ep.commit();
	}

	// 获取数据库更新时间
	public String getUpdateTime() {
		return sp.getString("brandUpdateTime", context.getResources()
				.getString(R.string.brand_update_time));
	}

	public void setHomeUpdateTime(String updateTime) {
		ep.putString("homeUpdateTime", updateTime);
		ep.commit();
	}

	// 获取主頁更新时间
	public String getHomeUpdateTime() {
		return sp.getString("homeUpdateTime", "1900-1-1");
	}

	// 是否重新导入数据库
	public boolean getIsInputBrandDB() {
		return sp.getBoolean("isInputBrandDB", true);
	}

	public void setIsInputBrandDb(boolean isInputBrandDb) {
		ep.putBoolean("isInputBrandDB", isInputBrandDb);
		ep.commit();
	}

	// zs新增 是否需要重新导入首页资讯数据库信息
	public boolean getIsInputHomeInfoDB() {
		return sp.getBoolean("isInputHomeInfoDB", true);
	}

	public void setIsInputHomeInfoDB(boolean isInputHomeInfoDB) {
		ep.putBoolean("isInputHomeInfoDB", isInputHomeInfoDB);
		ep.commit();
	}

	public String getNewProductLastTime() {
		return sp.getString("newProductLastTime", "");
	}

	public void setNewProductLastTime(String LastTime) {
		ep.putString("newProductLastTime", LastTime);
		ep.commit();
	}

	public void setUserSex(String sex) {
		ep.putString("sex", sex);
		ep.commit();
	}

	public String getUserSex() {
		return sp.getString("sex", "-1");
	}

	public void setUserBirthday(String birthday) {
		ep.putString("birthday", birthday);
		ep.commit();
	}

	public String getUserBirthday() {
		return sp.getString("birthday", "");
	}

	public void setUserEmail(String email) {
		ep.putString("email", email);
		ep.commit();
	}

	public String getUserEmail() {
		return sp.getString("email", "");
	}

	public void setUserEmailVerified(String EmailVerified) {
		ep.putString("EmailVerified", EmailVerified);
		ep.commit();
	}

	public String getUserEmailVerified() {
		return sp.getString("EmailVerified", "False");
	}

	public void setUserMobilePhone(String MobilePhone) {
		ep.putString("MobilePhone", MobilePhone);
		ep.commit();
	}

	public String getUserMobilePhone() {
		return sp.getString("MobilePhone", "");
	}

	public void setUserMobilePhoneVerified(String MobilePhoneVerified) {
		ep.putString("MobilePhoneVerified", MobilePhoneVerified);
		ep.commit();
	}

	public String getUserMobilePhoneVerified() {
		return sp.getString("MobilePhoneVerified", "False");
	}

	public void setUserID(String UserID) {
		ep.putString("UserID", UserID);
		ep.commit();
	}

	public String getUserID() {
		return sp.getString("UserID", "");
	}

	public void setUserLoginToken(String LoginToken) {
		ep.putString("LoginToken", LoginToken);
		ep.commit();
	}

	public String getUserLoginToken() {
		return sp.getString("LoginToken", "");
	}

	public void setUserErrorCode(String ErrorCode) {
		ep.putString("ErrorCode", ErrorCode);
		ep.commit();
	}

	public String getUserErrorCode() {
		return sp.getString("ErrorCode", "");
	}

	public void setUserMessage(String Message) {
		ep.putString("Message", Message);
		ep.commit();
	}

	public String getUserMessage() {
		return sp.getString("Message", "");
	}

	public void setUserType(String Type) {
		ep.putString("Message", Type);
		ep.commit();
	}

	public String getUserType() {
		return sp.getString("Type", "");
	}

	public void setUserStatus(String Status) {
		ep.putString("Status", Status);
		ep.commit();
	}

	public String getUserStatus() {
		return sp.getString("Status", "");
	}

	public void setUserNickName(String NickName) {
		ep.putString("NickName", NickName);
		ep.commit();
	}

	public String getUserNickName() {
		return sp.getString("NickName", "");
	}

	public void setUserPictureUrl(String PictureUrl) {
		if (PictureUrl.equals("")) {
			ep.putString("PictureUrl", "1");
			ep.commit();
		} else {
			ep.putString("PictureUrl", PictureUrl);
			ep.commit();
		}
	}

	public String getUserPictureUrl() {
		return sp.getString("PictureUrl", "");
	}

	// 用户登录信息
	public void setUserLoginName(String name) {
		ep.putString("username", name);
		ep.commit();
	}

	public String getUserLoginName() {
		return sp.getString("username", "");
	}

	public void setUserPassword(String passwrod) {
		ep.putString("password", passwrod);
		ep.commit();
	}

	public String getUserPasswrod() {
		return sp.getString("password", "");
	}

	public void setUserPoint(String point) {
		ep.putString("point", point);
		ep.commit();
	}

	public String getUserPoint() {
		return sp.getString("point", "");
	}

	// 存城市版本
	public void setCityVersion(int cityVersion) {
		ep.putInt("CityVersion", cityVersion);
		ep.commit();
	}

	public int getCityVersion() {
		return sp.getInt("CityVersion", 1);
	}

	/**
	 * 新浪微博信息
	 */
	public void setSinaWeiboUserID(String SinaWeiboUserID) {
		ep.putString("SinaWeiboUserID", SinaWeiboUserID);
		ep.commit();
	}

	public String getSinaWeiboUserID() {
		return sp.getString("SinaWeiboUserID", "");
	}

	public void setSinaWeiboNickName(String SinaWeiboNickName) {
		ep.putString("SinaWeiboNickName", SinaWeiboNickName);
		ep.commit();
	}

	public String getSinaWeiboNickName() {
		return sp.getString("SinaWeiboNickName", "");
	}

	public void setSinaWeiboToken(String SinaWeiboToken) {
		ep.putString("SinaWeiboToken", SinaWeiboToken);
		ep.commit();
	}

	public String getSinaWeiboToken() {
		return sp.getString("SinaWeiboToken", "");
	}

	public void setSinaWeiboExpireTime(String SinaWeiboExpireTime) {
		ep.putString("SinaWeiboExpireTime", SinaWeiboExpireTime);
		ep.commit();
	}

	public String getSinaWeiboExpireTime() {
		return sp.getString("SinaWeiboExpireTime", "");
	}

	/**
	 * 腾讯登录信息
	 */

	public void setQQNickName(String QQNickName) {
		ep.putString("QQNickName", QQNickName);
		ep.commit();
	}

	public String getQQNickName() {
		return sp.getString("QQNickName", "");
	}

	public void setQQOpenID(String QQOpenID) {
		ep.putString("QQOpenID", QQOpenID);
		ep.commit();
	}

	public String getQQOpenID() {
		return sp.getString("QQOpenID", "");
	}

	public void setQQToken(String QQToken) {
		ep.putString("QQToken", QQToken);
		ep.commit();
	}

	public String getQQToken() {
		return sp.getString("QQToken", "");
	}

	public void setQQExpireTime(String QQExpireTime) {
		ep.putString("QQExpireTime", QQExpireTime);
		ep.commit();
	}

	public String getQQExpireTime() {
		return sp.getString("QQExpireTime", "");
	}

	// 新增用户验证类型
	public void setVerifyType(String verifyType) {
		ep.putString("VerifyType", verifyType);
		ep.commit();
	}

	public String getVerifyType() {
		return sp.getString("VerifyType", "");
	}

	// 增加商场下拉刷新列表的刷新时间
	public void setRefreshTime(String refreshTime) {
		ep.putString("RefreshTime", refreshTime);
		ep.commit();
	}

	public String getRefreshTime() {
		return sp.getString("RefreshTime", "");
	}

	// 增加品牌下拉刷新列表的刷新时间
	public void setBrandRefreshTime(String refreshTime) {
		ep.putString("BrandRefreshTime", refreshTime);
		ep.commit();
	}

	public String getBrandRefreshTime() {
		return sp.getString("BrandRefreshTime", "");
	}

	// 存取软件版本
	public void setVersion(String version) {
		ep.putString("version", version);
		ep.commit();
	}

	public String getVersion() {
		return sp.getString("version", "2013.7.3.1");
	}

	// 存取商场刷新时间
	public void setShopUpdateTime(String shopUpdateTime) {
		ep.putString("shopUpdateTime", shopUpdateTime);
		ep.commit();
	}

	public String getshopUpdateTime() {
		return sp.getString("shopUpdateTime", "1900-1-1");
	}

	// 存取品牌刷新时间
	public void setBrandUpdateTime(String brandUpdateTime) {
		ep.putString("brandUpdateTime", brandUpdateTime);
		ep.commit();
	}

	public String getBrandUpdateTime() {
		return sp.getString("brandUpdateTime", context.getResources()
				.getString(R.string.brand_update_time));
	}

	// 存取weibo token
	public void setWeiBoToken(String weiBoToken) {
		ep.putString("weiBoToken", weiBoToken);
		ep.commit();
	}

	public String getWeiBoToken() {
		return sp.getString("weiBoToken", "");
	}

	// 存取商场微博id
	public void setShopWeiBoId(String shopWeiBoId) {
		ep.putString("shopWeiBoId", shopWeiBoId);
		ep.commit();
	}

	public String getShopWeiBoId() {
		return sp.getString("shopWeiBoId", "3537610947578965");
	}

	// 获取weiboID
	public String getWeiboCityID() {
		return sp.getString("WeiboCityID", "");
	}

	public void setWeiboCityID(String weibocityID) {
		ep.putString("WeiboCityID", weibocityID);
		ep.commit();
	}

	// 存取城市更新时间
	public String getCityUpdateTime() {
		return sp.getString("cityUpdateTime", "2013-5-1");
	}

	public void setCityUpdateTime(String cityUpdateTime) {
		ep.putString("cityUpdateTime", cityUpdateTime);
		ep.commit();
	}

	public boolean getIsFirstLoadHome() {
		return sp.getBoolean("isFirstLoadHome", true);
	}

	public void setIsFirstLoadHome(boolean isFirstLoadHome) {
		ep.putBoolean("isFirstLoadHome", isFirstLoadHome);
		ep.commit();
	}

	public String getHomeUpdateDateBase() {
		return sp.getString("homeUpdateDateBase", "19000101");
	}

	public void setHomeUpdateDateBase(String homeUpdateDateBase) {
		ep.putString("homeUpdateDateBase", homeUpdateDateBase);
		ep.commit();
	}

	public String getShopIds() {
		return sp.getString("ShopIds", "");
	}

	public void setShopIds(String shopIds) {
		ep.putString("ShopIds", shopIds);
		ep.commit();
	}

	/**
	 * 首页是否需要引导页
	 * 
	 * @return
	 */
	public boolean getIsGuide() {
		return sp.getBoolean("guide", true);
	}

	public void setIsGuide(boolean guide) {
		ep.putBoolean("guide", guide);
		ep.commit();
	}

	/*
	 * 上传DeviceToken标记
	 */
	public boolean getIsUpLoadToken() {
		return sp.getBoolean("isUpLoadToken", false);
	}

	public void setIsUpLoadToken(boolean isUpLoadToken) {
		ep.putBoolean("isUpLoadToken", isUpLoadToken);
		ep.commit();
	}

	/*
	 * 控制定制消息是否重复启动
	 */
	public int getServiceNum() {
		return sp.getInt("serviceNum", 0);
	}

	public void setServiceNum(int num) {
		ep.putInt("serviceNum", num);
		ep.commit();
	}

	/*
	 * 普通用户流水号
	 */
	public String getNormalTrace() {
		return sp.getString("normaltrace", "1000000000");
	}

	public void setNormalTrace(String num) {
		ep.putString("normaltrace", num);
		ep.commit();
	}

	/*
	 * pos机流水号
	 */
	public String getPosIP() {
		return sp.getString("posIP", "");
	}

	public void setPosIP(String ip) {
		ep.putString("posIP", ip);
		ep.commit();
	}
	
	
	/**
	 * pos mac地址
	 */
	public String getPosMac() {
		return sp.getString("mac", "");
	}

	public void setPosMac(String mac) {
		ep.putString("mac", mac);
		ep.commit();
	}
	
	
	/**
	 * pos流水号 形式：自增，最终流水号
	 */
	public String getPosTrace() {
		return sp.getString("postrace", "000000000000000000");
	}

	public void setPosTrace(String trace) {
		ep.putString("postrace", trace);
		ep.commit();
	}
	

}
